<template>
  <view class="content">
    <!-- <nav-header /> -->
    <view class="tab-card flex-row-start" :style="{top: `${getTop()}`}">
      <view class="tab-item" :class="{'tab-item-active': tabId == 1}" @click="setTabId(1)">未完成</view>
      <view class="tab-item" :class="{'tab-item-active': tabId == 2}" @click="setTabId(2)">已完成</view>
    </view>
	<view class="s-input-wrap sinput-fixed-w">
		<view class="s-input-box flex-row sinput-fixed">
			<input auto-focus="true" v-model="inputCode" @confirm="handleInput" class="s-input flex1" placeholder="扫描 / 输入收货单号" />
			<image v-if="inputCode" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="handleClear"></image>
			<image class="s-input-icon" src="/static/images/scan.png"></image>
		</view>
	</view>
    <view class="list-card-wrap" style="margin-top: 190rpx;">
      <view v-for="(item,index) in tableData" v-if="tableData.length" :key="index" class="list-card" @click="toPage(item)">
        <view class="flex-row-between">
          <view @click.stop="toDetail(item)" class="flex-row">
            <text class="gray-text">收货单号</text>
            <text class="black-text-34" style="margin-left: 20rpx;">{{item.bizNo}}</text>
            <uni-icons type="right" size="16" color="#8997A8"></uni-icons>
          </view>
        </view>
        <view class="flex-row-between">
          <view class="flex-row">
            <text class="gray-text">货&ensp;主</text>
            <text class="black-text-34" style="margin-left: 20rpx;">{{item.owner}}</text>
          </view>
          <view class="flex-row">
            <text class="gray-text">仓&ensp;库</text>
            <text class="black-text-34" style="margin-left: 20rpx;">{{item.stockCode}}</text>
          </view>
        </view>
        <view class="flex-row-between">
          <view class="flex-row">
            <text class="gray-text">库&ensp;存&ensp;状&ensp;态</text>
            <text class="black-text-34"
                  style="margin-left: 20rpx;">{{getDict(item.stockStatus, erpStockStatus)}}</text>
          </view>
        </view>
        <view class="content flex-row">
          <view class="flex-column flex1">
            <text class="gray-text">到货总数</text>
            <text class="black-text">{{item.totalQty}}</text>
          </view>
          <view class="split"></view>
          <view class="flex-column flex1">
            <text class="gray-text">收货总数</text>
            <text class="black-text">{{item.checkTotalQty || 0}}</text>
          </view>
        </view>
        <view v-if="tabId == 1" class="flex-row-between">
          <text class="gray-text">创建时间 {{item.createTime}}</text>
          <view v-if="item.actualQty==0" class="flex-row">
            <navigator :url="'/pages/inventoryAdj/purchase/edit?id='+item.id"
                       hover-class="other-navigator-hover">
              <view class="botton p-botton">收货</view>
            </navigator>
          </view>
          <view v-else class="flex-row">
            <navigator :url="'/pages/inventoryAdj/purchase/edit?id='+item.id"
                       hover-class="other-navigator-hover">
              <view class="botton">收货</view>
            </navigator>
            <view @click="completeTask(item)" class="botton p-botton">完成</view>
          </view>
        </view>
        <view v-if="tabId == 2" class="">
          <view class="flex-row-between">
            <text class="gray-text">创建时间</text>
            <text class="gray-text">{{item.createTime}}</text>
          </view>
          <view class="flex-row-between">
            <text class="gray-text">完成时间</text>
            <text class="gray-text">{{item.completeTime}}</text>
          </view>
        </view>
      </view>

	  <view class="no-table-data"v-if="tableData.length < 1" style="margin-top: 180rpx;">
	  	<text>暂无数据</text>
	  </view>
      <list-footer :total="total" :dataLength="tableData.length"></list-footer>
    </view>
  </view>
</template>

<script>
import {
  getDict,
  getTop
} from '@/utils/common.js'
import {
  getInStockBatchInfo,
  complete,
  getAdjustBillList,
  inoutstock
} from "./api"
var printModule = uni.requireNativePlugin("pda-print-PdaPrintModule")
export default {
  data() {
    return {
	  inputCode:"",
      getTop,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      searchValue: '',
      tabId: 1,
      tableData: [],
      erpStockStatus: [],
      getDict
    }
  },
  onNavigationBarSearchInputChanged(obj) {
    if (!obj.text) {
      this.queryParams.no = '';
      this.refreshList()
    }
  },
  onNavigationBarSearchInputConfirmed(obj) {
    // obj.text
    this.queryParams.no = obj.text;
    this.refreshList()
  },
  // 上拉加载
  onReachBottom() {
    if (this.tableData.length == this.total) {
      return;
    }
    this.queryParams.pageNum++;
    this.getList();
  },
  onLoad(option) {
    let that = this
    uni.$on('refreshList', function(data) {
      that.refreshList()
    })
    this.getDicts("erp_stock_status").then(res => {
      this.erpStockStatus = res.data;
    });
    // this.getList()
  },
  onUnload() {
    uni.$off('refreshList')
  },
  onShow() {
  	this.refreshList()
  },
  methods: {
	  handleClear(){
	  	this.inputCode = ''
		this.getList('search');
	  },
	  handleInput(){
	  	this.getList('search');
	  },
    toPage(item) {
      if (this.tabId == 2) {
        this.toDetail(item)
      }
    },
    toDetail(item) {
      this.$tab.navigateTo("/pages/inventoryAdj/purchase/detail?id=" + item.id);
    },
    setTabId(tabId) {
      this.tabId = tabId;
      this.refreshList();
    },
    clearForm() {
      this.form = {
        batchNo: '',
        storehouseLocationNo: '',
        productNo: '',
        productName: '',
        quantity: ''
      }
    },
    // 验证大于等于1的整数
    setNum(e) {
      if (e.target.value) {
        let newValue = e.target.value.replace(/^\D*(\d*).*$/g, "$1")
        newValue = Math.max(1, newValue)
        this.$nextTick(() => {
          this.form.quantity = newValue
        })
      }
    },
    getInStockBatchInfo(e) {
      if (e.target.value === "") {
        this.$modal.msg("请输入批次号")
        this.clearForm()
      } else {
        getInStockBatchInfo(e.target.value).then(res => {
          if (res.code == 200) {
            this.form = res.data
          }
        })
      }
    },
    refreshList() {
      this.tableData = [];
      this.total = 0;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList(type) {
      uni.showLoading({
        title: '加载中...'
      });
      if (this.tabId == 2) {
        this.queryParams.searchFinish = 'Y'
      } else {
        this.queryParams.searchFinish = 'N'
      }
	  this.queryParams.bizNo = this.inputCode
      getAdjustBillList(this.queryParams).then(res => {
       if(type == 'search'){
       	this.tableData = res.rows
       }else{
       	this.tableData = this.tableData.concat(res.rows);
       	this.total = res.total;
       }

       uni.hideLoading();
      });
    },
    submit() {
      if (!this.form.batchNo) {
        this.$modal.msg("请输入批次号")
      } else if (!this.form.storehouseLocationNo) {
        this.$modal.msg("请输入库位编码")
      } else if (!this.form.quantity || this.form.quantity == 0) {
        this.$modal.msg("请输入数量")
      } else {
        this.$modal.loading("入库中...")
        inoutstock({
          ...this.form,
          type: 0,
          operation: 0
        }).then(res => {
          this.$modal.closeLoading()
          if (res.code == 200) {
            this.$modal.msgSuccess("入库成功")
            this.clearForm()
          }
        })
      }
    },
    // 完成收货任务
    completeTask(item) {
      this.$modal.confirm('是否确认完成').then(() => {
        let detailData = [];
		this.$modal.loading("加载中...")
        complete({
          bizNo: item.bizNo
        }).then(res => {
				this.$modal.closeLoading()
          if (res.code == 200) {
            this.$modal.msgSuccess("操作成功");
            setTimeout(() => {
              this.refreshList();
            }, 1000);
          } else {
            this.$modal.confirmClose(res.msg);
          }
        })
      })
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #F8F8F8;
}
</style>
<style lang="scss" scoped>
.content {
  // margin: 17rpx 25rpx;
}
</style>
